package test.baojia.act.business;

import com.alibaba.fastjson.JSONObject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.Assert;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * 在Windows下，利用执行exe文件，完成EasyUpload的操作。
 * 1.查看au3/upload.au3，修改文件全路径为本地文件全路径。
 * 2.将.au3文件编译为.exe。
 * 3.修改properties文件下的 "upload.exe" property 。
 * 4.修改properties文件下的 "easyupload.uploadedFiles.firstSpan.text" property。
 */
public class EasyUploadBusiness extends Business {

    @Override
    public void run(JSONObject context, WebDriver driver, Properties resource, JSONObject parentContext) throws  Exception  {
        super.run(context, driver, resource, parentContext);

        //点击“选择文件”按钮。
        final String startButtonCssSelector = resource.getProperty("easyupload.startButton");
        final By startButtonBy = By.cssSelector(startButtonCssSelector);
        this.newWebDriverWait(null).until(
                ExpectedConditions.elementToBeClickable(startButtonBy)
        ).click();

        //选择文件。
        String exeFilePath = resource.getProperty("easyupload.exe");
        List<WebElement> newFileItemBefore = newFileItems();
        try {
            Runtime.getRuntime().exec(exeFilePath);
        } catch (IOException e) {
            e.printStackTrace();
            Assert.assertTrue(false , e.getMessage());
        }
        //选择文件后，检查是否有增加待上传文件数量。
        this.newWebDriverWait(null).until(
                ExpectedConditions.numberOfElementsToBe(newFileItemsBy() , newFileItemBefore.size() + 1)
        );

        //执行上传。
        //检查是否有删除、上传按钮。
        String deleteButtonCssSelector = resource.getProperty("easyupload.deleteButton");
        WebElement deleteWE = driver.findElement(By.cssSelector(deleteButtonCssSelector));
        Assert.assertEquals(deleteWE.getText() , resource.getProperty("easyupload.deleteButton.text"));
        String uploadButtonCssSelector = resource.getProperty("easyupload.uploadButton");
        WebElement uploadWE = driver.findElement(By.cssSelector(uploadButtonCssSelector));
        Assert.assertEquals(uploadWE.getText() , resource.getProperty("easyupload.uploadButton.text"));

        List<WebElement> uploadedFileItemsBefore = uploadedFileItems();
        uploadWE.click();

        //定位到已上传文件的位置，否则不在可视范围。
        String aduitUpload = this.context.getString("aduitUpload");
        if("true".equals(aduitUpload)){
            this.scroll2ElementTop(driver.findElement(By.cssSelector(resource.getProperty("easyupload.uploadedFiles.aduitRow"))));
        }else{
            this.scroll2ElementTop(driver.findElement(By.cssSelector(resource.getProperty("easyupload.uploadedFiles.row"))));
        }


        //上传后，检查是否有增加上传文件数量。
        final int allUnloadedCount = uploadedFileItemsBefore.size() + 1;
        this.newWebDriverWait(null).until(
                ExpectedConditions.numberOfElementsToBe(uploadedFileItemsBy() , allUnloadedCount)
        );
        //上传后，检查是否有超链接和文件名。
        this.newWebDriverWait(null).until(
                ExpectedConditions.textToBe(
                        By.cssSelector(String.format(resource.getProperty("easyupload.uploadedFiles.firstSpan") , allUnloadedCount)),
                        resource.getProperty("easyupload.uploadedFiles.firstSpan.text")
                )
        );
    }

    protected By newFileItemsBy(){
        String selector = resource.getProperty("easyupload.newFiles");
        return By.cssSelector(selector);
    }

    protected List<WebElement> newFileItems(){
        return driver.findElements(newFileItemsBy());
    }

    protected By uploadedFileItemsBy(){
        String selector = resource.getProperty("easyupload.uploadedFiles");
        return By.cssSelector(selector);
    }

    protected List<WebElement> uploadedFileItems(){
        return driver.findElements(uploadedFileItemsBy());
    }
}
